/**
 * <b>项目名：</b>hclv-web<br/>  
 * <b>包名：</b>com.flea.hclv.controller<br/>  
 * <b>文件名：</b>HHotelController.java<br/>  
 * <b>版本信息：</b>1.0<br/>  
 * <b>日期：</b>2018年9月12日 下午3:29:31<br/>  
 * <b>COPYRIGHT 2010-2012 ALL RIGHTS RESERVED 虫子科技</b>-版权所有<br/>
 */
package com.flea.hclv.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.flea.hclv.model.entity.HAddress;
import com.flea.hclv.model.entity.HHotel;
import com.flea.hclv.model.entity.HResourceRelation;
import com.flea.hclv.service.HHotelService;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.ControllerResult;

/**
 * @description
 * @createTime 2018年9月12日 下午3:29:31
 * @modifyTime
 * @author myflea@163.com
 * @version 1.0
 */
@Controller
@RequestMapping("/flea/hclv/hotel/")
public class HHotelController {
	@Autowired
	private HHotelService hotelService;

	Logger logger = LoggerFactory.getLogger(HHotelController.class);

	@RequestMapping("saveOrUpdateHotel")
	public void saveOrUpdateHotel(HttpServletRequest request,
			HttpServletResponse response) {
		String hotelJSON = request.getParameter("model");

		String userId = FleaOrgUtils.getCurUserUuid(request);
		HHotel hotel = JSON.parseObject(hotelJSON, HHotel.class);
		HAddress address = JSON.parseObject(hotelJSON, HAddress.class);
		/**
		 * 保存图片
		 */
		String resource = request.getParameter("files");
		List<HResourceRelation> filesRelation = JSON.parseArray(resource,
				HResourceRelation.class);

		boolean result = hotelService.saveOrUpdate(hotel, address, userId,
				filesRelation);

		ControllerResult cResult = new ControllerResult(result, "操作成功", result);

		cResult.WriteToPage(response);
	}
	/**
	 * 获取所有酒店信息包括分页
	 * @description
	 * @methodName queryAll
	 * @param
	 * @returnType void   
	 * @throw
	 */
	@RequestMapping("/queryAllForPage")
	public void queryAll(HttpServletRequest request, FleaPage<HHotel> paging,HttpServletResponse response) {

		String queryName = request.getParameter("queryName");
		String queryLevel = request.getParameter("queryLevel");
		String queryProvince = request.getParameter("queryProvince");
		String queryCity = request.getParameter("queryCity");
		String queryZone = request.getParameter("queryZone");

		FleaPage<HHotel> pageList = hotelService.findForGird(queryName,queryLevel, queryProvince, queryCity, queryZone, paging.getCurPage(), paging.getPageSize());

		ControllerResult conresult = new ControllerResult(pageList);
		conresult.set(true, "操作成功");

		conresult.WritePagingDataToPage(response, pageList);

	}
	@RequestMapping("/querySelfAllForPage")
	public void querSelyAllForPage(HttpServletRequest request, FleaPage<HHotel> paging,HttpServletResponse response) {
		
		String queryName = request.getParameter("queryName");
		String queryLevel = request.getParameter("queryLevel");
		String queryProvince = request.getParameter("queryProvince");
		String queryCity = request.getParameter("queryCity");
		String queryZone = request.getParameter("queryZone");
		
		FleaPage<HHotel> pageList = hotelService.findForGirdBySelf(request,queryName,queryLevel, queryProvince, queryCity, queryZone, paging.getCurPage(), paging.getPageSize());
		
		ControllerResult conresult = new ControllerResult(pageList);
		conresult.set(true, "操作成功");
		
		conresult.WritePagingDataToPage(response, pageList);
		
	}
	/**
	 * 获取自己的所有酒店，不带有分页信息
	 * @description
	 * @methodName queryAllByOrgId
	 * @param
	 * @returnType void   
	 * @throw
	 */
	@RequestMapping("/getSelfHotels")
	public void queryAllByOrgId(HttpServletRequest request,
			HttpServletResponse response) {
		String userId = FleaOrgUtils.getCurUserUuid(request);
		boolean isSuper = FleaOrgUtils.isSuperAdmin(userId);
		List<HHotel> allHotels = new ArrayList<HHotel>();
		/**
		 * 如果是超级用户，返回所有
		 */
		if (isSuper) {
			allHotels = hotelService.queryAll();
		} else {
			OrgDept org = FleaOrgUtils.getCurrentUserDept(userId);
			String orgId = org.getDeptUuid();
			allHotels = hotelService.queryByOrgId(orgId);
		}

		ControllerResult conresult = new ControllerResult(allHotels);
		conresult.set(true, "操作成功");

		conresult.WritePagingDataToPage(response, allHotels);
	}

	@RequestMapping("/queryById")
	public void queryById(HttpServletRequest request,
			HttpServletResponse response) {

	}

	@RequestMapping("deletByIds")
	public void deletHotel(HttpServletRequest request,
			@Param("ids") String ids, HttpServletResponse response) {

		String userId = FleaOrgUtils.getCurUserUuid(request);
		boolean result = hotelService.deleteByIds(ids, userId);

		ControllerResult cResult = new ControllerResult(result,
				result == true ? "操作成功" : "操作失败", result);
		cResult.WriteToPage(response);
	}
}
